﻿
var Tools = {

    point: {

        parse: function(value) {
            var value = value.split(",");
            return {
                x: parseInt(value[0]),
                y: parseInt(value[1])
            };
        }
    },

    rectangle: {

        parse: function(value) {
            var value = value.split(",");
            return {
                x: parseInt(value[0]),
                y: parseInt(value[1]),
                width: parseInt(value[2]),
                height: parseInt(value[3])
            };
        },

        toString: function(value) {
            return value.x + ',' + value.y + ',' + value.width + ',' + value.height;
        }
    },

    date: {

        parse: function(value) {
            return new Date(parseInt(value.substr(6, value.length - 6 - 2)));
        },

        toHuman: function(date) {
            var current = new Date();
            var text = '';
            var eventdate = Math.floor((date.getTime() - 60000 * current.getTimezoneOffset()) / (24 * 3600 * 1000));
            var today = Math.floor(current.getTime() / (24 * 3600 * 1000));
            if (eventdate == today) {
                text += 'Today';
            }
            else if (eventdate == today - 1) {
                text += 'Yesterday';
            }
            else if (eventdate == today + 1) {
                text += 'Tomorrow';
            }
            else {
                text += (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
            }
            var h = date.getHours();
            var m = date.getMinutes();
            if (h < 10) {
                h = '0' + h;
            }
            if (m < 10) {
                m = '0' + m;
            }
            text += ' at ' + h + ':' + m;
            return text;
        },

        toHumanDelay: function(time) {
            time = Math.floor(time / 1000);
            var text = '';

            if (time >= 3600) {
                var hours = Math.floor(time / 3600);
                text += hours + ' Hour' + (hours > 1 ? 's' : '') + ' ';
                time = time % 3600;
            }
            if (time >= 60) {
                var minutes = Math.floor(time / 60);
                text += minutes + ' Minute' + (minutes > 1 ? 's' : '') + ' ';
                time = time % 60;
            }
            if (time > 0) {
                var seconds = time;
                text += seconds + ' Second' + (seconds > 1 ? 's' : '') + ' ';
                time = time % 60;
            }


            return text;
        }
    }
}